package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.properties.MinioProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.utils.MinioUtilS;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "套餐接口")
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 查询套餐
     */
    @ApiOperation("查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> query(@PathVariable Integer id) {
        SetmealVO setmealVO = setmealService.query(id);
        return Result.success(setmealVO);
    }

    /**
     * 分页查询套餐
     */
    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
//    @Cacheable(cacheNames = "adminSetmealCache", key = "#categoryId")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult res = setmealService.page(setmealPageQueryDTO);
        return Result.success(res);
    }

    /**
     * 新增套餐
     */
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#ids") // 删除套餐
    public Result del(@RequestParam List<Integer> ids) {
        setmealService.del(ids);
        return Result.success();
    }

    /**
     * 修改套餐
     */
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐的起售和停售
     */
    @ApiOperation("套餐的起售和停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealService.update(setmeal);
        return Result.success();
    }
}
